<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="名称" prop="name" :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id" :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
              <SelectTree ref="floor" :props="{
                          value: 'id',             // ID字段名
                          label: 'showName',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }" url="/smis/floor/jfFloor/treeData" :value="inputForm.floor.id" :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.floor.id=value; $refs.selectPoint.showFloor(value); inputForm.x = ''; inputForm.y = ''; $refs.selectPoint.removeMarkPoint();}" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="x轴" prop="x" :rules="[
                  {required: true, message:'x轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.x" placeholder="请填写x轴"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="y轴" prop="y" :rules="[
                  {required: true, message:'y轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.y" placeholder="请填写y轴"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="z轴" prop="z" :rules="[
                  {required: true, message:'z轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.z" placeholder="请填写z轴"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位类型" prop="pointType">
              <el-select v-model="inputForm.pointType" placeholder="请选择点位类型" style="width: 100%;">
                <el-option label="巡更" value="巡更"></el-option>
                <el-option label="巡检" value="巡检"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="inputForm.pointType === '巡检'">
            <el-form-item label="正常状态说明" prop="normalStatusDescription">
              <el-input v-model="inputForm.normalStatusDescription" placeholder="请填写正常状态说明" type="textarea" :rows="2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="inputForm.pointType === '巡检'">
            <el-form-item label="正常状态图示" prop="normalStatusImages">
              <el-upload
                :action="`${$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/smis/bookshelf/normalStatus`"
                :headers="{token: $cookie.get('token')}"
                list-type="picture-card"
                :file-list="normalStatusImageList"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="(response, file, fileList) => {
                  inputForm.normalStatusImages = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                }"
                :disabled="method==='view'">
                <i class="el-icon-plus" v-if="method!=='view'"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
            </el-form-item>
          </el-col>
          <el-col :span="24" style="height: 400px;">
            <el-form-item label="所在位置" prop="x" :rules="[
                {required: true, message:'请选择所在位置', trigger:'blur'}
               ]">
              <select-point ref="selectPoint" type="room" @onMapClick="mapClick"></select-point>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        dialogVisible: false,
        dialogImageUrl: '',
        normalStatusImageList: [],
        inputForm: {
          id: '',
          name: '',
          floor: {
            id: ''
          },
          x: '',
          y: '',
          z: '',
          pointType: 'patrol', // 默认为巡更
          normalStatusDescription: '',
          normalStatusImages: '',
          ext1: '',
          ext2: '',
          ex3: ''
        }
      }
    },
    components: {
      SelectTree,
      SelectPoint
    },
    methods: {
      init (method, id, floorId) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建点位`
        } else if (method === 'edit') {
          this.title = '修改点位'
        } else if (method === 'view') {
          this.title = '查看点位'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/bookshelf/jfBookshelf/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              this.inputForm = this.recover(this.inputForm, data.jfBookshelf)
              this.loading = false
              this.$refs.selectPoint.showFloorAndMarkPoint(this.inputForm.floor.id, this.inputForm.x, this
                .inputForm.y, this.inputForm.z)
              // 初始化图片列表
              if (this.inputForm.normalStatusImages) {
                this.initNormalStatusImageList()
              }
            })
          } else if (floorId !== '') {
            this.inputForm.floor.id = floorId
            this.$refs.selectPoint.showFloor(floorId)
          }
          this.$refs.selectPoint.resize()
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            const formData = {
              ...this.inputForm,
              floorId: this.inputForm.floor.id // 确保传递floorId
            }
            this.$http({
              url: `/smis/bookshelf/jfBookshelf/save`,
              method: 'post',
              data: formData
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
        this.inputForm.z = e.z
      },
      // 初始化正常状态图片列表
      initNormalStatusImageList () {
        this.normalStatusImageList = []
        if (this.inputForm.normalStatusImages) {
          const imageUrls = this.inputForm.normalStatusImages.split('|')
          imageUrls.forEach((url, index) => {
            if (url) {
              this.normalStatusImageList.push({
                name: `图片${index + 1}`,
                url: url
              })
            }
          })
        }
      },
      // 图片预览
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 移除图片
      handleRemove (file, fileList) {
        this.inputForm.normalStatusImages = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
      }
    }
  }
</script>
